package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.exception.LogicException;
import cn.wolfcode.wolf2w.query.RegionQueryObject;
import cn.wolfcode.wolf2w.redis.service.IRedisService;
import cn.wolfcode.wolf2w.redis.test.RedisByKey;
import cn.wolfcode.wolf2w.repository.RegionRepository;
import cn.wolfcode.wolf2w.service.IRegionService;
import cn.wolfcode.wolf2w.util.AssertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.CriteriaDefinition;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.UUID;

@Service
public class RegionServiceImpl implements IRegionService {

    @Autowired
    private RegionRepository regionRepository;
    @Autowired
    private IRedisService redisService;
    @Autowired
    private MongoTemplate template;

    @Override
    public void insert(Region region) {
        regionRepository.insert(region);
    }

    @Override
    public void delete(String id) {
        regionRepository.deleteById(id);
    }

    @Override
    public void update(Region region) {
        regionRepository.save(region);
    }

    @Override
    public Region selectOne(String id) {
        Region region = regionRepository.findById(id).orElse(null);
        //Region region = byId.get();
        return region;
    }

    @Override
    public List<Region> selectAll() {
        List<Region> all = regionRepository.findAll();
        return all;
    }

    @Override
    public Page<Region> query(RegionQueryObject qo) {
        //查询封装类
        Query query = new Query();
        //查询数据总数
        Long count = template.count(query, Region.class);
        //判断查询总数是否为空
        if(count==null) {
            //若为空，返回一个空集合
            return Page.empty();
        }
        //引入分页插件
        Pageable pageable=PageRequest.of(qo.getCurrentPage()-1, qo.getPageSize(), Sort.Direction.DESC,"_id");
        query.with(pageable);
        List<Region> regions = template.find(query, Region.class);
        return new PageImpl<>(regions,pageable,count);
    }

    @Override
    public void saveOrUpdate(Region region) {
        if(StringUtils.hasLength(region.getId())){
            this.update(region);
        }else{
            //save
            this.insert(region);
        }
    }

    @Override
    public void changeHotValue(int hot, String id) {
        ////全量修改
        ////1、查询当前对象
        //Region region = this.selectOne(id);
        ////2、给当前对象设置热门状态
        //region.setIshot(hot);
        ////3、更新
        //this.update(region);

        //部分字段修改
        //1、创建拼接对象
        Query query=new Query();
        //2、拼接条件
        query.addCriteria(Criteria.where("_id").is(id));
        //3、创建Update对象
        Update update=new Update();
        //4、修改特定字段
        update.set("ishot", hot);
        //5、执行修改语句
        template.updateMulti(query, update, Region.class);
    }

    @Override
    public List<Region> queryHotRegion() {
        //通过region的常量字段来作为查询热门区域的参数
        List<Region> list=regionRepository.queryByIshotOrderBySequence(Region.STATE_HOT);
        return list;
    }


}
